<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../static/static/css/font.css">
    <link rel="stylesheet" href="../static/static/css/weadmin.css">
    <script src="../static/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="weadmin-body">
    <form class="layui-form" onsubmit="return false;">
        <div class="layui-row">
            <div class="layui-inline">
                <select id="type" name="type" lay-verify="required">
                    <option value="">全部</option>
                    <option value="text">文字</option>
                    <option value="image">图片</option>
                </select>
            </div>
            <div class="layui-inline">
                <select id="status" name="status" lay-verify="required">
                    <option value="">全部</option>
                    <option value="use">进行中</option>
                    <option value="delete">已删除</option>
                </select>
            </div>
            <button class="layui-btn" onclick="getNewData()"><i class="layui-icon">&#xe615;</i></button>
            <button class="layui-btn" onclick="addTextNotice()"><i class="layui-icon"></i>新增广播公告
            </button>
            <button class="layui-btn" onclick="addImageNotice()"><i class="layui-icon"></i>新增图片轮播
            </button>
        </div>
    </form>
    <table id="list" lay-filter="test"></table>
</div>
<script>
    var tableIns;
    layui.use('table', function () {
        //执行渲染
        tableIns = layui.table.render({
            elem: '#list'
            , id: 'list'
            , page: true
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'type', title: '类型'}
                , {field: 'content', title: '文本内容/图片地址'}
                , {field: 'status', title: '状态'}
                , {field: 'indexNum', title: '顺序'}
                , {fixed: 'right', align: 'center', toolbar: '#bar'}
            ]]
            , toolbar: true
            , url: '/shopNotice/getNoticeList'
            , method: 'post'
            , contentType: 'application/json'
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.status, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.infos //解析数据列表
                };
            }
        });

        layui.table.on('tool(test)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'detail') {

            } else if (layEvent === 'del') {
                layer.confirm('确认删除?', function (index) {
                    layer.close(index);
                    layui.$.ajax({
                        type: "GET",
                        url: "/shopNotice/deleteNotice",
                        data: {
                            id: data.id
                        },
                        dataType: "json",
                        success: function (data) {
                            if (data.status != 0) {
                                var layer = layui.layer;
                                layer.open({
                                    title: '提示'
                                    , content: data.msg
                                });
                            } else {
                                getNewData();
                            }
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                handleNotice(data.id,null,data.content,'/shopNotice/updateNotice');
            }else if (layEvent === 'indexUp') {
                changeOrder(data.id,'up');
            }else if (layEvent === 'indexDown') {
                changeOrder(data.id,'down');
            }
        });
    });
    var changeOrder = function (id,type) {
        var $ = layui.$;
        var loadIng = layer.load(2);
        $.ajax({
            type: "GET",
            url: "/shopNotice/changeOrder",
            data: {
                type: type,
                id: id
            },
            dataType: "json",
            contentType: 'application/json',
            success: function (data) {
                if (data.status != 0) {
                    layer.close(loadIng);
                    layer.open({
                        title: '提示'
                        , content: data.msg
                    });
                } else {
                    layer.close(loadIng);
                    getNewData();
                }
            }
        });
    }
    var addTextNotice = function () {
        handleNotice(null,'text','请输入广播内容','/shopNotice/addNotice');
    }
    var addImageNotice = function () {
        handleNotice(null,'image','请输入合法的图片链接','/shopNotice/addNotice');
    }

    var handleNotice = function (id,type,tit,url) {
        var html = '<textarea id="content" name="content" placeholder="'+tit+'" ' +
                'style="margin-top: 0px; margin-bottom: 0px; height: 170px;"' +
                ' class="layui-textarea"></textarea>'
        var alert = layer.open({
            title: '新增公告'
            , content: html
            , area: ['500px', '300px']
            , yes: function (index, layero) {
                var $ = layui.$;
                var loadIng = layer.load(2);
                var model = {
                    content: document.getElementById("content").value,
                    type: type,
                    id: id
                }
                $.ajax({
                    type: "POST",
                    url: url,
                    data: JSON.stringify(model),
                    dataType: "json",
                    contentType: 'application/json',
                    success: function (data) {
                        if (data.status != 0) {
                            layer.close(loadIng);
                            layer.open({
                                title: '提示'
                                , content: data.msg
                            });
                        } else {
                            layer.close(loadIng);
                            layer.close(alert);
                            getNewData();
                        }
                    }
                });
            }
        });
    }
    var getNewData = function () {
        tableIns.reload({
            where: {
                type: document.getElementById("type").value,
                status: document.getElementById("status").value
            }
            , page: {
                curr: 1
            }
        });
    }
</script>
<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="indexUp">顺序提前</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="indexDown">顺序延后</a>
</script>
</body>
</html>